package org.tloss.web.editor.form.editors.command;

import java.util.Collections;
import java.util.List;

import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;
import org.tloss.web.editor.enity.editors.JavaClassPropertyDescriptor;
import org.tloss.web.editor.model.command.Assign;
import org.tloss.web.editor.utils.Variable;
import org.tloss.web.editor.utils.VariableBrowser;
import org.tloss.web.editor.utils.VariableProvider;

public class AssignProperty implements IPropertySource {
	private IPropertyDescriptor[] descriptors;
	public static final String NAME_PRO = "NAME_PRO";
	public static final String VARIABLE_PRO = "VARIABLE_PRO";
	public static final String TYPE_PRO = "TYPE_PRO";
	private Assign command;
	private IResourceChangeListener listener;

	public AssignProperty() {
		descriptors = new IPropertyDescriptor[3];
		descriptors[0] = new PropertyDescriptor(VARIABLE_PRO, "Value") {
			public CellEditor createPropertyEditor(Composite parent) {
				DialogCellEditor cellEditor = new DialogCellEditor(parent) {
					@SuppressWarnings("unchecked")
					@Override
					protected Object openDialogBox(Control arg0) {
						VariableBrowser dialog = new VariableBrowser(
								arg0.getShell());
						if (listener instanceof VariableProvider) {
							dialog.setSupportedVariable(((VariableProvider) listener)
									.getCurrentCommandVariables());
						} else {
							dialog.setSupportedVariable(Collections.EMPTY_LIST);
						}
						if (dialog.open() == Window.OK) {
							List<Variable> variables = dialog.getVariable();
							if (variables != null && variables.size() > 0) {
								Variable variable = variables.get(0);
								return variable;
							}
						}
						return null;
					}
				};
				return cellEditor;
			};
		};
		descriptors[1] = new TextPropertyDescriptor(NAME_PRO, "Name");
		descriptors[2] = new JavaClassPropertyDescriptor(TYPE_PRO, "Type");
	}

	public Assign getCommand() {
		return command;
	}

	public void setCommand(Assign command) {
		this.command = command;
	}

	public IResourceChangeListener getListener() {
		return listener;
	}

	public void setListener(IResourceChangeListener listener) {
		this.listener = listener;
	}

	@Override
	public Object getEditableValue() {
		return this;
	}

	@Override
	public IPropertyDescriptor[] getPropertyDescriptors() {
		return descriptors;
	}

	@Override
	public Object getPropertyValue(Object arg0) {
		if (NAME_PRO.equals(arg0)) {
			return command.getName() != null ? command.getName() : "";
		} else if (VARIABLE_PRO.equals(arg0)) {
			return command.getValue() != null ? (command.getValue()
					.getDisplayText() != null ? command.getValue()
					.getDisplayText() : "") : "";
		} else if (TYPE_PRO.equals(arg0)) {
			return command.getType() != null ? command.getType() : "";
		}
		return "";
	}

	@Override
	public boolean isPropertySet(Object arg0) {
		return false;
	}

	@Override
	public void resetPropertyValue(Object arg0) {

	}

	@Override
	public void setPropertyValue(Object arg0, Object arg1) {
		if (NAME_PRO.equals(arg0)) {
			command.setName(arg1.toString());
			listener.resourceChanged(null);
		} else if (VARIABLE_PRO.equals(arg0)) {
			if (arg1 != null && arg1 instanceof Variable) {
				Variable variable = (Variable) arg1;
				command.setValue(variable.convertToMathOperand());
				listener.resourceChanged(null);
			}
		} else if (TYPE_PRO.equals(arg0)) {
			command.setType(arg1.toString());
			listener.resourceChanged(null);
		}

	}

}
